@page "/CustomRenderer"
@using BlazorDatasheet.Edit.DefaultComponents
@using BlazorDatasheet.Render
@using BlazorDatasheet.Core.Interfaces
@using BlazorDatasheet.Core.Data
@using BlazorDatasheet.DataStructures.Geometry

<PageTitle>Custom Renderer</PageTitle>
<h1>CustomRenderer</h1>

<p>Below is an example of a custom renderer that shows a progress bar between 0 and 200</p>

<Datasheet Sheet="sheet" CustomCellTypeDefinitions="CustomTypes"></Datasheet>

@code {
    
    private Sheet sheet;
    private Dictionary<string, CellTypeDefinition> CustomTypes { get; } = new();

    protected override void OnInitialized()
    {
        CustomTypes.Add("progress", CellTypeDefinition.Create<TextEditorComponent, ProgressRenderer>());
        sheet = new Sheet(1, 3);
        sheet.Cells[0, 0].Value = 0;
        sheet.Cells[0, 1].Value = 100;
        sheet.Cells[0, 1].Value = 200;
        sheet.Cells.SetType(new RowRegion(0), "progress");


        base.OnInitialized();
    }

    private class NumBetweenZeroAndTwoHundredValidator : IDataValidator
    {
        public bool IsValid(object? value)
        {
            if (value == null)
                return false;
            if (double.TryParse(value.ToString(), out double num))
                return num >= 0 && num <= 200;
            else
                return false;
        }

        public bool IsStrict { get; } = true;
        public string Message => "The value must be between zero and two hundred";
    }

}